<?php
abstract class Sosweb_Controller_Action extends Zend_Controller_Action 
{
	protected $config;
	protected $logger;
	protected $db;
	protected $sessionNamespace;
	
   	public function init() 
   	{
   		//setup firebug logger
        $this->logger = new Sosweb_Logger('../logs/szkolajazdy.log');
        
   		try
		{
			//load configuration
			$this->config = new Zend_Config_Ini('../application/config.ini', 'general');
			
			set_include_path('.' . PATH_SEPARATOR . $this->config->path->models . PATH_SEPARATOR . get_include_path());
			
			// setup database 
			$this->db = new Sosweb_Db_Adapter_Pdo_Mysql($this->config->db->params);
			Zend_Db_Table::setDefaultAdapter($this->db);
			$this->db->setFetchMode(Zend_Db::FETCH_OBJ);
						
	       	//initialize session
	   		Zend_Session::start();
	   		$this->initSession(null);
	   		
	   		//set common variables
	   		$this->view->current_page = $this->getRequest()->getControllerName();
	   		$this->view->current_action = $this->getRequest()->getActionName();
		}
   		catch(Zend_Db_Adapter_Exception $ex)
		{
			$this->logger->error('Błąd przy połączeniu do bazy danych '.$ex->getMessage());
		}		
		catch(Zend_Exception $ex)
		{
			$this->logger->error($ex->getMessage());
		}
   	}
   	
   	public function initSession($namespace)
   	{
   	    if($namespace === null)
   	    {
   	        $this->sessionNamespace = new Zend_Session_Namespace('Default');
   	    }
   	    else
   	    {
            $this->sessionNamespace = new Zend_Session_Namespace($namespace);   	        
   	    }
   	}

   	public function getSession()
   	{
   	    if(Zend_Session::isStarted())
   	    {
   	        return $this->sessionNamespace;
   	    }
   	    else
   	    {
   	        return null;
   	    }
   	}
}